//
//  WNPrivacyView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/4.
//

import UIKit

class WNPrivacyView: UIView, UITextViewDelegate {
    
    static let shared = WNPrivacyView()
    
    lazy var bgView: UIView = {
        let view = UIView.init(frame: CGRect.zero)
        view.backgroundColor = .white
        view.wn_cornerWithBorder(10)
        return view
    }()
    
    lazy var privacyLabel: UILabel = {
        let label = UILabel()
        label.text = "Dear User"
        label.font = .wn_font(name: .bold, size: 18)
        label.textColor = .init(named: "black_101010")
        label.textAlignment = .center
        return label
    }()
    
    lazy var refuseBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle(" Cancel ", for: .normal)
        btn.setTitleColor(.init(named: "gray_CECECE"), for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 15)
        btn.titleLabel?.textAlignment = .center
        btn.backgroundColor = .init(named: "white_F3F3F3")
        btn.rx.tap.bind { [weak self] in
            UIView.animate(withDuration: 0.15) {
                if cydia && applications {
                    assert(false)
                }else {
                    exit(0)
                }
            }
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(18)
        return btn
    }()
    
    lazy var agreeBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("Agree", for: .normal)
        btn.backgroundColor = .init(named: "red_FE95B4")
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 18)
        btn.rx.tap.bind { [weak self] in
            saveIsAgreePrivacy(true)
            hidenPrivacy()
        }.disposed(by: rx.disposeBag)
        btn.wn_cornerWithBorder(18)
        return btn
    }()
    
    lazy var textView: IQTextView = {
        let view = IQTextView()
        view.font = .wn_font(name: .bold, size: 14)
        view.showsVerticalScrollIndicator = false
        view.wn_appendLinkString(string: "Before you use the \(getAppName()), please carefully read and understand the ")
        view.wn_appendLinkString(string: "Service Agreement", withURLString: WNConfigure.WNURL.wn_Agreement)
        view.wn_appendLinkString(string: " And ")
        view.wn_appendLinkString(string: "Privacy Policy", withURLString: WNConfigure.WNURL.wn_Privacy)
        view.wn_appendLinkString(string: ".\nWe care about your privacy and data security. We keep this app free by showing ads. Can we continue to use your data to tailor ads for you?\nYou can change your choice anytime in the app settings. Our partners will collect data and use a unique identifier on your device to show you ads.")
        view.textColor = .black
        view.linkTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.init(named: "red_FE95B4")!]
        view.isEditable = false
        view.backgroundColor = .white
        view.delegate = self
        return view
    }()
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .init(white: 0, alpha: 0.55)
        addSubview(bgView)
        bgView.addSubview(privacyLabel)
        bgView.addSubview(refuseBtn)
        bgView.addSubview(agreeBtn)
        bgView.addSubview(textView)
        bgView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(280)
            make.height.equalTo(320)
        }
        privacyLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(14)
        }
        refuseBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(-65)
            make.bottom.equalTo(-20)
            make.size.equalTo(CGSize(width: 110, height: 36))
        }
        agreeBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview().offset(65)
            make.bottom.equalTo(-20)
            make.size.equalTo(CGSize(width: 110, height: 36))
        }
        textView.snp.makeConstraints { (make) in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(44)
            make.bottom.equalTo(-68)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        wn_getTopVC()?.tabBarController?.wn_pushWebVC(URL.absoluteString, URL.absoluteString == WNConfigure.WNURL.wn_Privacy ? "Privacy Policy" : "Service Agreement")
        return false
    }
}

func showPrivacy() {
    WNPrivacyView.shared.frame = Screen.bounds
    wn_getTopVC()?.tabBarController?.view.addSubview(WNPrivacyView.shared)
}

func hidenPrivacy() {
    WNPrivacyView.shared.removeFromSuperview()
}
